﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.IO;
using System.Data;

public partial class Gallery : System.Web.UI.Page
{
    public string UrlFolderPath
    {
        get
        {
            string _urlFolderPath = Request.QueryString["folderPath"];
            if (!string.IsNullOrEmpty(_urlFolderPath))
                return _urlFolderPath;
            else
                return null;
        }
    }
    private string hiddenExtensionFolder = ConfigurationManager.AppSettings["HiddenExtensionFolder"];
    private string[] acceptedImageExtensions = new string[] { ".jpg", ".jpeg", ".bmp", ".png", ".gif" };

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            if (!string.IsNullOrEmpty(UrlFolderPath))
            {
                galleryFoldersUC.Visible = false;
                LoadPictures();
            }
    }

    private void LoadPictures()
    {
        DataTable imagesDT = new DataTable();
        imagesDT.Columns.Add("ImagePath");
        imagesDT.Columns.Add("SmallImagePath");
        imagesDT.Columns.Add("ImageName");

        string serverFolderPath = Server.MapPath(UrlFolderPath);
        if (Directory.Exists(serverFolderPath))
        {
            DirectoryInfo di = new DirectoryInfo(serverFolderPath);
            if (!di.Name.ToLower().EndsWith(hiddenExtensionFolder))
            {
                foreach (FileInfo fileInfo in di.GetFiles())
                {
                    if (File.Exists(Server.MapPath(UrlFolderPath + "/Small" + fileInfo.Name)))
                    {
                        if (acceptedImageExtensions.Contains(fileInfo.Extension.ToLower()))
                        {
                            DataRow dr = imagesDT.NewRow();
                            dr["ImagePath"] = UrlFolderPath.Replace("~/", "") + "/" + fileInfo.Name;
                            dr["SmallImagePath"] = UrlFolderPath.Replace("~/", "") + "/Small" + fileInfo.Name;
                            dr["ImageName"] = fileInfo.Name;
                            imagesDT.Rows.Add(dr);
                        }
                    }
                }

                rptGalleryPhotos.DataSource = imagesDT;
                rptGalleryPhotos.DataBind();

                lnkGalleryTop.Visible = lnkGalleryBottom.Visible = true;
            }
            else
            {
                lblHidden.Visible = true;
            }
        }
        else
        {
            lblHidden.Visible = true;
        }
    }
}